package cn.maihe.elg.operation.config;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

import static com.google.code.kaptcha.Constants.*;

/**
 * @Description
 * @Author caixh
 * @Date 2021/6/7.
 */
@Configuration
@Setter
@Getter
public class KaptchaProperties {
    @Value("${kaptcha.border:'no'}")
    private String border;
    @Value("${kaptcha.width:150}")
    private String width;
    @Value("${kaptcha.height:42}")
    private String height;
    @Value("${kaptcha.textproducer.font.size:28}")
    private String fontSize;
    @Value("${kaptcha.textproducer.font.color:black}")
    private String fontColor;
    @Value("${kaptcha.background.clear.from:92,189,170}")
    private String bgFrom;
    @Value("${kaptcha.background.clear.to:255,255,255}")
    private String bgTo;
    @Value("${kaptcha.textproducer.char.space:4}")
    private String charSpace;
    @Value("${kaptcha.textproducer.char.length:4}")
    private String charLength;

    @Bean(name = "captchaProducer")
    public DefaultKaptcha getKaptchaBean()
    {
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties properties = new Properties();
        // 是否有边框 默认为true 我们可以自己设置yes，no
        properties.setProperty(KAPTCHA_BORDER, border);
        // 验证码文本字符颜色 默认为Color.BLACK
        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, fontColor);

        properties.setProperty(KAPTCHA_NOISE_IMPL, "com.google.code.kaptcha.impl.NoNoise");
        // 验证码图片宽度 默认为200
        properties.setProperty(KAPTCHA_IMAGE_WIDTH, width);
        // 验证码图片高度 默认为50
        properties.setProperty(KAPTCHA_IMAGE_HEIGHT, height);
        // 验证码文本字符大小 默认为40
        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, fontSize);
        // KAPTCHA_SESSION_KEY
        properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCode");
        // 验证码文本字符长度 默认为5
        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, charLength);
        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, charSpace);
        // 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
//        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");
        properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");

        // 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy
        properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.WaterRipple");

        properties.setProperty(KAPTCHA_BACKGROUND_CLR_FROM,bgFrom);
        properties.setProperty(KAPTCHA_BACKGROUND_CLR_TO,bgTo);

        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
}
